package com.xiaoj;

public class ThreadDemo5 {

    public static void main(String[] args) {
        GrabVotes grabVotes = new GrabVotes();

        //创建两个线程同时抢票
        Thread thread1 = new Thread(grabVotes);
        Thread thread2 = new Thread(grabVotes);
        thread1.start();
        thread2.start();
    }
}

class GrabVotes implements Runnable{

    private Integer ticketNums = 10;

    @Override
    public void run() {

        while(true){
            if(ticketNums <= 0){
                System.out.println("票被抢完了");
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "抢到一张票，" + "剩余票数：" + (--ticketNums));

        }
    }
}
